define(["angular",
    "framework/utils/JSONPrettyFormat",
    'framework/dataGrid/DataGridModule',
    'framework/pagination/GillionPaginationModule',
    'framework/msg/GillionMsgModule'
], function (angular, JSONPrettyFormat) {
    /**
     *  Module
     *
     * Description
     */
    var BusinessModelAttributeModule = angular.module('BusinessModelAttributeTestModule', ['DataGridModule',
        'GillionPaginationModule',
        'GillionMsgModule'
    ]);
    BusinessModelAttributeModule.controller('BusinessModelAttributeController', function ($scope, Resource, $dataSourceManager, GillionMsg, $location,$compile) {
        $scope.businessModelId='1111111111';
        $scope.rightNone=true;
        $scope.rightManager=false;
        $scope.addOper=false;
        $scope.editOper=false;
        var businessModelAttributeResource = Resource("/grule-manager/businessModelAttribute/:id", {
            id: "@id"
        });
        var childAttributeResource = Resource("/grule-manager/businessModelAttribute/childList/:id", {
            id: "@id"
        });
        function refresh(){
            Resource("/grule-manager/businessModelAttribute/list/:id", {id: '@id'}).get({
                id:  $scope.businessModelId
            }, function (response) {
                if(response.data!=null){
                    $scope.modelAttributes=response.data;
                }
            });
        }
        $scope.addAttribute = function (){
            $scope.rightNone=false;
            $scope.rightManager=true;
            $scope.addOper=true;
            $scope.editOper=false;
            var attribute={};
            $scope.attribute = attribute;
        };

        $scope.add=function(){
            $scope.attribute.businessModelId=$scope.businessModelId;
            businessModelAttributeResource.create($scope.attribute, function (response) {
                if(response.success){
                    alert("新增成功！");
                    refresh();
                }
            });

        };

        $scope.addSon=function(id){
            $scope.rightNone=false;
            $scope.rightManager=true;
            $scope.addOper=true;
            $scope.editOper=false;
            var attribute={};
            $scope.attribute = attribute;
            $scope.attribute.businessModelId=$scope.businessModelId;
            $scope.attribute.parentId=id;


        };

        $scope.edit=function(id){
            businessModelAttributeResource.update($scope.attribute, function (response) {
                if(response.success){
                    alert("修改成功！");
                    refresh();
                }
            });

        }
        $scope.delete=function(id){
            businessModelAttributeResource.delete({id:id}, function (response) {
                if(response.success){
                    alert("删除成功！");
                    refresh();
                }
            });

        }

        $scope.editAttribute = function (id,name,dataType,valueType,valueExpress,parentId){
            $scope.rightNone=false;
            $scope.rightManager=true;
            $scope.addOper=false;
            $scope.editOper=true;
            var attribute={};
            attribute.id=id;
            attribute.name=name;
            attribute.dataType=dataType;
            attribute.valueType=valueType;
            attribute.valueExpress=valueExpress;
            attribute.parentId=parentId;
            $scope.attribute = attribute;
        };

        Resource("/grule-manager/businessModelAttribute/list/:id", {id: '@id'}).get({
            id:  $scope.businessModelId
        }, function (response) {
            if(response.data!=null){
                $scope.modelAttributes=response.data;
            }
        });

        $scope.viewObject = function (id,margin,name,dataType,valueType,valueExpress) {
            $scope.rightNone=false;
            $scope.rightManager=true;
            $scope.addOper=false;
            $scope.editOper=true;
            var attribute={};
            attribute.id=id;
            attribute.name=name;
            attribute.dataType=dataType;
            attribute.valueType=valueType;
            attribute.valueExpress=valueExpress;
            $scope.attribute = attribute;
            if($("#"+id).text()=="对象[-]"){
                angular.element("#son"+id).text("");
                $("#"+id).text("对象[+]");
                return;
            }
            $("#"+id).text("对象[-]");
            childAttributeResource.get({
                id: id
            }, function (response) {
                if (response.success) {
                    var records = response.data;
                    angular.forEach(records, function (item){
                        if(item.dataType!='Object'){
                            angular.element("#son"+id).append(
                                $compile(
                                    "<div class='model-value model-value-border' style='margin-left:"+20*margin+"px' >"+
                                    "<div class='model-value-l'><button type='button' class='engine-btn-model' id='"+item.id+"' ng-click='editAttribute(&apos;"+item.id+"&apos;,&apos;"+item.name+"&apos;,&apos;"+item.dataType+"&apos;,&apos;"+item.valueType+"&apos;,&apos;"+item.valueExpress+"&apos;)' >"+item.dataType+"</button></div>"+
                                    "<div class='model-value-r'>"+item.name+"</div>"+
                                    "<div class='model-value-right'>"+
                                    "<span class='results'>"+item.valueExpress+"</span>"+
                                    "<span class='remove'>"+item.valueType+"</span>"+
                                    "</div>"+
                                    "<div class='clear'></div>"+
                                    "</div>"
                                )($scope)
                            )
                        }else{
                            angular.element("#son"+id).append(
                                $compile(
                                    "<div class='model-value  model-value-border' style='margin-left:"+20*margin+"px' >"+
                                    "<div class='model-value-l'><button type='button' class='engine-btn-model' id='"+item.id+"' ng-click='viewObject(&apos;"+item.id+"&apos;,"+(margin+1)+",&apos;"+item.name+"&apos;,&apos;"+item.dataType+"&apos;,&apos;"+item.valueType+"&apos;,&apos;"+item.valueExpress+"&apos;)' >对象[+]</button></div>"+
                                    "<div class='model-value-r'>"+item.name+"</div>"+
                                    "<div class='model-value-right'>"+
                                    "<span class='results'>"+item.valueExpress+"</span>"+
                                    "<span class='plus'><a ><i class='icon-engine-plus'></i></a></span>"+
                                    "<span class='remove'>"+item.valueType+"</span>"+
                                    "</div>"+
                                    "<div class='clear'></div>"+
                                    " </div>"+
                                    "<div id='son"+item.id+"'></div>"

                                )($scope)
                            )
                        }
                    })
                }
            });
        };


    });

    BusinessModelAttributeModule.filter('dataTypeFilter', function() {
        return function(input) {
            var dataTypeDict = {
                'Integer' : '整型',
                'String' : '字符串',
                'Float' : '浮点型',
                'Date' : '时间',
                'Boolean' : '布尔',
                'Object' : '对象类型'
            };
            return dataTypeDict[input] || '';
        };
    });

    BusinessModelAttributeModule.filter('valueTypeFilter', function() {
        return function(input) {
            var valueTypeDict = {
                'Constant' : '常量',
                'JDBC' : 'JDBC',
                'Service' : 'java接口',
                'Rule' :  'Rule',
                'InParameter' : '传参',
                'Variable' : '变量',
                'RemoteService' : 'esb'
            };
            return valueTypeDict[input] || '';
        };
    });
    BusinessModelAttributeModule.filter('valueExpressFilter', function() {
        return function(input) {
            return input.replace("params[0].","").replace("Integer|","").replace("Float|","");
        };
    });
});